package com.ymt.fmd.platform.web.rest;

import java.util.Map;
import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.ymt.fmd.platform.service.BoDataService;
import com.ymt.fmd.util.Const;


@Component
@Path("/bodata")
public class BoDataResource {

	private Logger logger = Logger.getLogger(BoDataResource.class.getName());
	
	@Resource
	private BoDataService bos;
	
	@Context
	private HttpServletRequest request;
	
	/**
	 * get single bo
	 * @param tsId
	 * @param models
	 * @param timezoneoffset
	 * @return
	 */
	@GET
    @Path("/get")
    @Produces(MediaType.APPLICATION_JSON)
	public String get(@QueryParam("tsId")String tsId, 
			@QueryParam("models")String models,
			@QueryParam("timezoneoffset")String timezoneoffset) {
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		String tenantId = userDetail.get(Const.TENANT_ID);
		logger.finer("get tenantId="+tenantId);
		logger.finer("get tsId="+tsId);
		logger.finer("get models="+models);
		logger.finer("get timezoneoffset="+timezoneoffset);
		String rtn = null;
		try {
			rtn = bos.get(tenantId, tsId, models, Integer.parseInt(timezoneoffset));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtn;
	}
	
	/**
	 * save single bo
	 * @param tsId
	 * @param data
	 * @param timezoneoffset
	 * @return
	 */
	@POST
    @Path("/save")
    @Produces(MediaType.APPLICATION_JSON)
	public String save(@FormParam("tsId")String tsId, 
			@FormParam("data")String data,
			@FormParam("timezoneoffset")String timezoneoffset) {
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		String tenantId = userDetail.get(Const.TENANT_ID);
		logger.finer("save tenantId="+tenantId);
		logger.finer("save tsId="+tsId);
		logger.finer("save data="+data);
		logger.finer("save timezoneoffset="+timezoneoffset);
		if (tsId==null && data==null) {	//IE bug workaround
			//logger.warning("no content received.");
			return "{\"success\":0, \"msg\":\"nocontent\"}";
		}
		String rtn = null;
		try {
			rtn = bos.save(tenantId, tsId, data, Integer.parseInt(timezoneoffset));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtn;
	}
	
	/**
	 * query bo data in dhx format with paging
	 * @param piId
	 * @param param
	 * @return
	 */
	@POST
    @Path("/queryDhx")
    @Produces(MediaType.APPLICATION_JSON)
	public String queryDhx(@FormParam("piId")String piId, 
			@FormParam("param")String param) {
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		String lang = userDetail.get(Const.LANG);
		String tenantId = userDetail.get(Const.TENANT_ID);
		logger.finer("queryDhx piId="+piId);
		logger.finer("queryDhx param="+param);
		logger.finer("queryDhx lang="+lang);
		logger.finer("queryDhx tenantId="+tenantId);
		if (piId==null && param==null) {	//可能是IE bug
			return "{\"success\":0, \"msg\":\"nocontent\"}";
		}
		String rtn = null;
		try {
			rtn = bos.queryDhx(tenantId, piId, param, lang);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtn;
	}
	
	/**
	 * query bo data in dhx format without paging
	 * @param piId
	 * @param param
	 * @return
	 */
	@POST
    @Path("/queryAllDhx")
    @Produces(MediaType.APPLICATION_JSON)
	public String queryAllDhx(@FormParam("piId")String piId, 
			@FormParam("param")String param) {
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		String lang = userDetail.get(Const.LANG);
		String tenantId = userDetail.get(Const.TENANT_ID);
		logger.finer("queryAllDhx piId="+piId);
		logger.finer("queryAllDhx param="+param);
		logger.finer("queryAllDhx lang="+lang);
		logger.finer("queryAllDhx tenantId="+tenantId);
		if (piId==null && param==null) {	//可能是IE bug
			return "{\"success\":0, \"msg\":\"nocontent\"}";
		}
		String rtn = null;
		try {
			rtn = bos.queryAllDhx(tenantId, piId, param, lang);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtn;
	}
	
	/**
	 * 分页查询返回bootstrap table数据
	 * @param piId
	 * @param param
	 * @return
	 */
	@POST
    @Path("/queryBt")
    @Produces(MediaType.APPLICATION_JSON)
	public String queryBt(@FormParam("piId")String piId, 
			@FormParam("param")String param) {
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		String tenantId = userDetail.get(Const.TENANT_ID);
		String lang = userDetail.get(Const.LANG);
		logger.finer("queryBt piId="+piId);
		logger.finer("queryBt param="+param);
		logger.finer("queryBt tenantId="+tenantId);
		logger.finer("queryBt lang="+lang);
		if (piId==null && param==null) {	//可能是IE bug
			return "{\"success\":0, \"msg\":\"nocontent\"}";
		}
		String rtn = null;
		try {
			rtn = bos.queryBt(tenantId, piId, param, lang);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtn;
	}
	
	/**
	 * save bo data in dhx format
	 * @param piId
	 * @param param
	 * @return
	 */
	@POST
    @Path("/saveAllDhx")
    @Produces(MediaType.APPLICATION_JSON)
	public String saveAllDhx(@FormParam("piId")String piId, 
			@FormParam("param")String param) {
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		String tenantId = userDetail.get(Const.TENANT_ID);
		String lang = userDetail.get(Const.LANG);
		logger.finer("saveAllDhx piId="+piId);
		logger.finer("saveAllDhx param="+param);
		logger.finer("saveAllDhx lang="+lang);
		logger.finer("saveAllDhx tenantId="+tenantId);
		if (piId==null && param==null) {	//可能是IE bug
			return "{\"success\":0, \"msg\":\"nocontent\"}";
		}
		String rtn = null;
		try {
			rtn = bos.saveAllDhx(tenantId, piId, param, lang);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtn;
	}
}
